﻿using AnlianCommon.Common.Domains.BankCustomers;
using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using SheepCommon.API.Seedweanings;

namespace SheepCommon.API.Vendors
{
    public class VendorSearchHander : IRequestHandler<VendorSearchCommand, ResultPage<Vendor>>
    {
        private readonly IBaseRepository<Vendor> _vendorRepository;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="vendorRepository"></param>
        /// <param name="mapper"></param>
        public VendorSearchHander(IBaseRepository<Vendor> vendorRepository, IMapper mapper)
        {
            _vendorRepository = vendorRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 供应商显示查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<Vendor>> Handle(VendorSearchCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<Vendor>() { Code = (int)CommonErrorCode.Search, TotalCount = 0, PagedCount = 0 };
            try
            {
                var vendor = await (await _vendorRepository.GetAll()).ToListAsync();
                if (!string.IsNullOrEmpty(request.VendorName) || !string.IsNullOrEmpty(request.Vmnemonic))
                {
                    vendor = vendor.Where(p => p.VendorName.Contains(request.VendorName) || p.Vmnemonic.Contains(request.Vmnemonic)).ToList();
                }
                if (!string.IsNullOrEmpty(request.VendorNo))
                {
                    vendor = vendor.Where(p => p.VendorNo == request.VendorNo).ToList();
                }
                if (!string.IsNullOrEmpty(request.VendorPhone))
                {
                    vendor = vendor.Where(p => p.VendorPhone == request.VendorPhone).ToList();
                }
                if (!string.IsNullOrEmpty(request.MarketName))
                {
                    vendor = vendor.Where(p => p.MarketName == request.MarketName).ToList();
                }
                if (!string.IsNullOrEmpty(request.SalesmanName))
                {
                    vendor = vendor.Where(p => p.SalesmanName == request.SalesmanName).ToList();
                }
                if (!string.IsNullOrEmpty(request.Vstatus))
                {
                    vendor = vendor.Where(p => p.Vstatus == request.Vstatus).ToList();
                }
                result.TotalCount = vendor.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                vendor = vendor.OrderByDescending(c => c.VendorId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = vendor;
            }
            catch (Exception ex)
            {
                throw;
            }
            return result;
        }
    }
}
